/*
 * @Author: maskMan
 * @LastEditTime: 2024-06-12 18:34:34
 * @Descripttion:网络请求管理
 */
import cacheStore from '../general/cacheStore'
import { AxiosInstance, AxiosStatic, RawAxiosRequestHeaders } from '../define/axios'
import Ax from './axios'
import { MARK } from '../define/variable'
import Config from '../general/config'
const JSONSTR = 'application/json'
const URLSTR = 'application/x-www-form-urlencoded'
const FORMSTR = 'multipart/form-data'
const axios = Ax as unknown as AxiosStatic
export default class Request {
  public static baseURL = Config.baseUrl
  private static _resInstance: AxiosInstance
  private static _defaultHead: RawAxiosRequestHeaders = {
    'Content-Type': JSONSTR
  }
  /**
   * @description: 创建请求实例
   * @return {*}
   */
  private static _createAxios() {
    try {
      this._resInstance = axios.create({ baseURL: this.baseURL, headers: this._defaultHead })
      // 响应返回参数格式
      this._resInstance.interceptors.response.use(
        (res: any) => {
          if (res?.data?.status?.token) {
            CacheStore.setPublicData(MARK.TOKEN, res?.data?.status?.token)
          }
          return res.data
        },
        (error) => {
          if (error.response) {
            return error.response
          }
          console.error(error.message)
          return null
        }
      )
      // 发送拦截
      this._resInstance.interceptors.request.use(
        (req: any) => {
          const token = CacheStore.getPublicData(MARK.TOKEN)
          if (token) {
            req.headers.setAuthorization(`Bearer ${token}`)
          }
          return req
        },
        (error) => {
          if (error.response) {
            return error.response
          }
          console.error(error.message)
          return null
        }
      )
    } catch (error) {
      console.error('请求返回错误数据', error)
    }
  }
  /**
   * @description: get 请求
   * @param {string} url 请求路由
   * @param {object} params 请求参数数据
   * @param {string} baseURL 请求地址
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static get(url: string, params: object, headers: RawAxiosRequestHeaders = this._defaultHead): Promise<Obj> {
    this._resInstance == undefined && this._createAxios()
    return this._resInstance
      .get(url, { params, baseURL: this.baseURL, headers, timeout: 8000 })
      .then(function (data: any) {
        return data
      })
      .catch(function (error: any) {
        console.log('请求错误', error)
        return null
      })
  }
  /**
   * @description: post 请求
   * @param {string} url 请求路由
   * @param {object} params 请求参数数据
   * @param {string} baseURL 请求地址
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static post(url: string, data: Obj, headers: RawAxiosRequestHeaders = this._defaultHead): Promise<Obj> {
    this._resInstance == undefined && this._createAxios()
    return this._resInstance
      .post(url, data, { baseURL: this.baseURL, headers, timeout: 8000 })
      .then((data: Obj) => {
        return data
      })
      .catch((error: any) => {
        console.log('请求错误', error)
        return null
      })
  }
  /**
   * @description: delete 请求
   * @param {string} url 请求路由
   * @param {object} params 请求参数数据
   * @param {string} baseURL 请求地址
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static delete(url: string, params: object, headers: RawAxiosRequestHeaders = this._defaultHead): Promise<Obj> {
    this._resInstance == undefined && this._createAxios()
    return this._resInstance
      .delete(url, { params, baseURL: this.baseURL, headers, timeout: 8000 })
      .then(function (data: any) {
        return data
      })
      .catch(function (error: any) {
        console.log('请求错误', error)
        return null
      })
  }
  /**
   * @description: put 请求
   * @param {string} url 请求路由
   * @param {object} params 请求参数数据
   * @param {string} baseURL 请求地址
   * @param {RawAxiosRequestHeaders} headers 请求头部
   * @return {*}
   */
  public static put(url: string, data: Obj, headers: RawAxiosRequestHeaders = this._defaultHead): Promise<Obj> {
    this._resInstance == undefined && this._createAxios()
    return this._resInstance
      .put(url, data, { baseURL: this.baseURL, headers, timeout: 8000 })
      .then((data: Obj) => {
        return data
      })
      .catch((error: any) => {
        console.log('请求错误', error)
        return null
      })
  }
}
